//
//  ViewController.swift
//  UITableViewCellAutoFitRowHeight
//
//  Created by Xie Wei on 2020/1/3.
//  Copyright © 2020 Xie Wei. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    let reuseIdentifier = NSStringFromClass(TableViewCell.self)
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(TableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
        return tableView
    }()
    
    lazy var dataArr = ["通过属性实现自动适配行高",
                        "通过代理实现自动适配行高"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "自动适配行高"
        view.addSubview(tableView)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        tableView.snp.makeConstraints { (make) in
            make.left.top.right.bottom.equalTo(view)
        }
    }
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
        cell.textLabel?.text = dataArr[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0:
            let vc = ViewController1()
            vc.title = dataArr[indexPath.row]
            navigationController?.pushViewController(vc, animated: true)
            
        case 1:
            let vc = ViewController2()
            vc.title = dataArr[indexPath.row]
            navigationController?.pushViewController(vc, animated: true)
            
        default:
            print("default")
        }
    }
}

